package yunmouren.top.attackdistance;


import net.minecraft.command.CommandBase;
import net.minecraft.command.ICommandSender;
import net.minecraft.entity.ai.attributes.AttributeModifier;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.text.TextComponentString;
import se.eris.notnull.NotNull;

public class Command extends CommandBase {
    @Override
    public @NotNull
    String getName() {
        return "AttackDistance";
    }

    @Override
    public @NotNull String getUsage(@NotNull ICommandSender sender) {
        return "AttackDistance <distance>";
    }

    @Override
    public int getRequiredPermissionLevel() {
        return 1;
    }

    @Override
    public void execute(@NotNull MinecraftServer server, @NotNull ICommandSender sender, String[] args) {
        if (args.length == 0) {
            sender.sendMessage(new TextComponentString("Values are required"));
        } else {
            /*
              拿到玩家 清空属性 应用属性
             */
            EntityPlayerMP Player = getPlayer(server, sender);
            try {

                Player.getEntityAttribute(EntityPlayer.REACH_DISTANCE).removeModifier(Player.getUniqueID());

                Player.getEntityAttribute(EntityPlayer.REACH_DISTANCE).applyModifier(
                        new AttributeModifier(Player.getUniqueID(), "attack-distance", new Double(args[0]), 0)
                );

            } catch (RuntimeException e) {
                sender.sendMessage(new TextComponentString("I'm sorry, I can't run him."));
            }

        }
    }

    /**
     * 拿到玩家实体
     */
    public EntityPlayerMP getPlayer(MinecraftServer server, ICommandSender sender) {
        return server.getPlayerList().getPlayerByUsername(sender.getName());
    }
}